Skip to content

argmin-negation-overflow: fix aten.min.dim indices at dtype-extremal minima - #43

Merged
gokulkrishna98 merged 1 commit into
apple:mainfrom
arames:user/arames/fuzz/argmin-negation-overflow
Jul 20, 2026
Merged

argmin-negation-overflow: fix aten.min.dim indices at dtype-extremal minima#43
gokulkrishna98 merged 1 commit into
apple:mainfrom
arames:user/arames/fuzz/argmin-negation-overflow

Conversation

@arames

@arames arames commented Jul 16, 2026

Copy link
Copy Markdown

Symptom

aten.min.dim returns the wrong indices when the true minimum is a
dtype-extremal integer value (e.g. uint8 min 0, int8 min -128). values
is correct; only indices is wrong. Silent — no crash.

Root cause

replace_min_dim in coreai_torch/_aten_to_core.py computed argmin as
argmax(x * -1). Negation via multiply is not order-reversing at integer
extremes: 0 * -1 == 0 (not the range top) for unsigned, and -128 * -1
overflows int8. So argmax scores the true minimum incorrectly.

Fix

Reverse order for integer dtypes with the bitwise complement ~x = x ^ -1
(broadcasting_bitwise_xor with an all-ones constant) instead of negation.
~x = -x - 1 is a strictly decreasing bijection over the full range of every
integer dtype (signed and unsigned), never overflows, and introduces no new
ties, so argmax(~x) == argmin(x) with first-index-on-tie semantics preserved.
The float path keeps x * -1 (exact, no overflow).

Tests

Adds dtype-extremal coverage (uint8 min 0; int8/int16/int32 dtype-min),
duplicate-minima tie-break, and a float case proving the float path is
unchanged. Reverting the fix fails all integer cases.

Comment thread tests/ops/test_ops.py
@arames
arames marked this pull request as ready for review July 16, 2026 23:54
Comment thread coreai_torch/_aten_to_core.py Outdated
@gokulkrishna98

Copy link
Copy Markdown
Contributor

Please install pre-commit, to resolve linting issues: https://github.com/apple/coreai-torch/blob/main/.pre-commit-config.yaml

Comment thread tests/ops/test_ops.py Outdated
@arames
arames force-pushed the user/arames/fuzz/argmin-negation-overflow branch from e9eb406 to b9e8cec Compare July 17, 2026 21:19
@arames

arames commented Jul 17, 2026

Copy link
Copy Markdown
Author

Installed pre-commit and re-ran it over the changed files (ruff check, ruff format, plus the generic hooks and license-header check) — all clean now. Squashed the branch down to a single commit as well.

@arames
arames force-pushed the user/arames/fuzz/argmin-negation-overflow branch from b9e8cec to 556fdd8 Compare July 17, 2026 22:32
@gokulkrishna98

Copy link
Copy Markdown
Contributor

Hi Alex, for merging, your commits must have verified signature. I think you have to setup signing-key ssh for this GitHub account.

@arames
arames force-pushed the user/arames/fuzz/argmin-negation-overflow branch from 556fdd8 to 048c67d Compare July 17, 2026 22:59
Core AI has argmax but not argmin, so replace_min_dim derives argmin
by negating x and taking argmax. Plain negation (x * -1) overflows at
integer dtype extremes (e.g. int8 min -128, or uint8 min 0), producing
wrong argmin indices. Reverse integer inputs with bitwise complement
(~x) instead, which is exact and overflow-free across the full integer
range; float/bool inputs keep the exact negation path.
@arames
arames force-pushed the user/arames/fuzz/argmin-negation-overflow branch from 048c67d to ae069d7 Compare July 17, 2026 23:07
@gokulkrishna98
gokulkrishna98 merged commit 35589fa into apple:main Jul 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants